package net.lgpage.activity;

import java.io.Serializable;

import com.matriq.cloudmanager.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import net.lgpage.tool.Dispatcher;
import net.lgpage.tool.GsonTool;
import net.lgpage.tool.ImageTool;
import net.lgpage.tool.SharedPreferencesTool;
import net.lgpage.tool.TagTool;
import net.lgpage.util.PromptTool.ToastUtil;
import net.lgpage.util.Saver;

@SuppressLint("NewApi")
public class BaseActivity extends Activity {
	public String Tag = getClass().getSimpleName();


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Saver.loadMyApp(savedInstanceState);
	}
	public static final int SESSION_OUT = 6666;
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == SESSION_OUT){
			setResult(SESSION_OUT);
			onSessionOut();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	public void onSessionOut(){
		finish();
	}
	@Override
	protected void onStart() {
		setBackView();
		super.onStart();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		Saver.saveMyApp(outState);
	}
	public void setError(String msg,View view){
		if(view instanceof EditText){
			EditText et = (EditText) view;
			if(msg !=null&&msg.length()>0)
			ToastUtil.noretryToast(this, msg);
		}
		/*else if(view instanceof TextInputLayout){
			TextInputLayout til = (TextInputLayout) view;
			til.setError(msg);
		}*/
	}
	private static View getRootView(Activity context) {
		return ((ViewGroup) context.findViewById(android.R.id.content))
				.getChildAt(0);
	}

	public void setBackView(){
		View view = findViewById(R.id.ll_back);
		if(view!=null){
			view.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					finish();
				}
			});
		}
	}
	public void loadImage(String url, ImageView iv) {
		ImageTool.loadImage(url, iv);
	}
	public void loadImage(String url,ImageView iv,DisplayImageOptions option){
		
		ImageTool.loadImageUseOption(url, iv, option);
	}
	public Object load(String key, Object defValue) {
		return new SharedPreferencesTool(this).load(key, defValue);
	}
	public Object userLoad(String key, Object defValue) {
		return new SharedPreferencesTool(this).userLoad(key, defValue);
	}

	public Object load(String name, String key, Object defValue) {
		return new SharedPreferencesTool(this).load(name, key, defValue);
	}

	public void putUserValue(String key, Object value) {
		new SharedPreferencesTool(this).putUserValue(key, value);
	}

	public void putAppValue(String key, Object value) {
		new SharedPreferencesTool(this).putAppValue(key, value);
	}

	public void putValue(String name, String key, Object value) {
		new SharedPreferencesTool(this).putValue(name, key, value);
	}

	public Activity getActivity() {
		return this;
	}

	public float getDensity() {
		return getResources().getDisplayMetrics().density;
	}

	public String intentGetObj() {
		return new Dispatcher(this).intentGetObj();
	}
	public Object intentGetObj(Object defVal) {
		return new Dispatcher(this).intentGetObj(defVal);
	}
	public Object intentGetObj(String key,Object defVal) {
		return new Dispatcher(this).intentGetObj(key,defVal);
	}

	public void toActivity(Class clazz, Object obj) {
		new Dispatcher(this).toActivity(clazz, obj);
	}

	public void toActivity(Class clazz) {
		new Dispatcher(this).toActivity(clazz);
	}

	public void toActivityForResult(Class clazz, int requestCode) {
		new Dispatcher(this).toActivityForResult(clazz, requestCode);
	}

	public void toActivityForResult(Class clazz, Object obj, int requestCode) {
		new Dispatcher(this).toActivityForResult(clazz, obj, requestCode);
	}


	public void resultBackWithString(int resultCode,String obj){
		Intent it = new Intent();
		it.putExtra("obj", obj);
		setResult(resultCode, it);
	}
	public void resultBackWithObj(int resultCode,Object obj){
		setResult(resultCode);
		if(obj!=null){
			Intent it = new Intent();
			it.putExtra("obj", GsonTool.getGson().toJson(obj));
			setResult(resultCode, it);
		}else{
			setResult(resultCode);
		}
		finish();
	}

	public void  resultBack(int resultCode){
		resultBackWithObj(resultCode, null);
	}
	public void toast(String str) {
		ToastUtil.noretryToast(getActivity(), str);
	}

	public void tag(String msg) {
		TagTool.v(Tag, msg);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Saver.saveCookies(this);
	}

}
